$(function(){
	var userKey = getcookie('key');
	var act = GetQueryString("act");
	if(userKey){
		window.location.href=WapSiteUrl+'/tmpl/member/member.html?act=member';
		return;
	}
	$(".content").html(template("login-template",{'WapSiteUrl':WapSiteUrl,'oauth_name':getcookie('oauth_name')}));
    //回到顶部
    $(".gotop").click(function (){
        $(window).scrollTop(0);
    });
    var key = getcookie('key');
	$('#logoutbtn').click(function(){
		var username = getcookie('username');
		var key = getcookie('key');
		var client = 'wap';
		$.ajax({
			type:'get',
			url:ApiUrl+'/index.php?act=logout',
			data:{username:username,key:key,client:client},
			success:function(result){
				if(result){
					delCookie('username');
					delCookie('key');
					location.href = WapSiteUrl+'/tmpl/member/login.html';
				}
			}
		});
	});
	//授权登录
	var referurl = document.referrer;//上级网址
	$("input[name=referurl]").val(referurl);
	$.sValid.init({
        rules:{
            username:"required",
            userpwd:"required"
        },
        messages:{
            username:"用户名必须填写！",
            userpwd:"密码必填!"
        },
        callback:function (eId,eMsg,eRules){
            if(eId.length >0){
                var errorHtml = "";
                $.map(eMsg,function (idx,item){
                    errorHtml += "<p>"+idx+"</p>";
                });
                $(".error-tips").html(errorHtml).show();
            }else{
                 $(".error-tips").html("").hide();
            }
        }  
    });
	$('#loginbtn').click(function(){//会员登陆
		var username = $('#username').val();
		var pwd = $('#userpwd').val();
		var client = 'wap';
		if($.sValid()){
	          $.ajax({
				type:'post',
				url:SiteUrl+"/index.php?act=login&op=oauth_login",
				data:{user_name:username,password:pwd,client:client,'form_submit':'ok'},
				dataType:'json',
				success:function(result){
					if(!result.datas.error){
						if(typeof(result.datas.key)=='undefined'){
							return false;
						}else{
							$.sDialog({
								skin:"green",
								content:"登录并绑定成功！",
								okBtn:false,
								cancelBtn:false,
								callback:function(){
									addcookie('username',result.datas.username);
									addcookie('key',result.datas.key);
									addcookie('member_level',result.datas.member_level);
									addcookie('member_channel',result.datas.member_channel);
									location.href = referurl;
								}
							});
						}
						$(".error-tips").hide();
					}else{
						$(".error-tips").html(result.datas.error).show();
					}
				}
			 });  
        }
	});
});